<?php
namespace Admin\Controller;
use Think\Controller;
class ManageController extends BaseController {
	public function __construct(){
		parent::__construct();
	}
   public function imageList(){
   	$image = M('image')->select();
   	$this -> assign('info',$image);
   	$this -> display();
   }
   //图片-添加
   public function imageAdd(){
   	if(IS_POST){	
   		$post = I('post.');
   		//上传代码
   		$cfg = array(
			'rootPath' => WORKING_PATH . UPLOAD_ROOT_PATH
			);
		//
		$upload = new \Think\Upload($cfg);
		$upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		//上传
		$info = $upload -> uploadOne($_FILES['img']);
		//判断上传的返回结果
		if($info){
			//设置表中filepath字段
			$post['url'] =  $info['savepath'] . $info['savename'];
			//设置表中hasfile字段
			$post['ctime'] =time();
			$rst = M('img')->add($post);
			if($rst){
				$this -> success('添加成功',U('ownerList'));
			}else{
				$this -> error('添加失败，请重新添加！！');
			}
		}else{
			$this -> error($upload->getError());
		}
   	}else{
   		$this -> display();
   	}
   }
   //图片 -删除
   public function imageDel(){
   	if(IS_POST){
   		$data = I('post.');
   		foreach($data['id'] as $v){
   			$rst = M('image')->where(array('id'=>$v))->delete();	
   		}
   		if($rst){
   			$a['info'] = '删除成功';
   			$a['flag'] = true;
   			$this -> ajaxReturn($a);
   		}else{   			
   			$a['info'] = '删除失败';
   			$a['flag'] = false;
   			$this -> ajaxReturn($a);
   		}
   	}else{
   		$rst = M('image')->where('id='.I('get.id'))->delete();
   		if($rst){
   			$this -> success('删除成功！',U('imageList'));
   		}else{
   				$this -> error('删除失败，请重新删除！！');
   		}
   	}
   }
   //图片-列表
   public function ownerList(){
    $info =  M('img')->select();
    foreach ($info as $key => $value) {
       $info[$key]['time'] = date('Y-m-d H:i:s',$value['ctime']);
    }
    $this -> assign('info',$info);
    $this->display();
   }
   //图片-删除
   public function ownerDel(){
         $rst = M('img')->where('id='.I('get.id'))->delete();
         if($rst){
            $this -> success('删除成功！',U('ownerList'));
         }else{
               $this -> error('删除失败，请重新删除！！');
         }
   }
   //文章管理-列表
   public function newsList(){
    $info =  M('news')->select();
    foreach ($info as $key => $value) {
       $info[$key]['time'] = date('Y-m-d H:i:s',$value['ctime']);
    }
    $this -> assign('info',$info);
      $this->display();
   }
   //文章管理-删除
   public function newsDel(){
         $rst = M('news')->where('id='.I('get.id'))->delete();
         if($rst){
            $this -> success('删除成功！',U('newsList'));
         }else{
               $this -> error('删除失败，请重新删除！！');
         }
   }
   //文章管理
   public function newsAdd(){
      if(IS_POST){   
         $post = I('post.');
        $cfg = array(
          'rootPath' => WORKING_PATH . UPLOAD_ROOT_PATH
          );
        //
        $upload = new \Think\Upload($cfg);
        $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        //上传
        $info = $upload -> uploadOne($_FILES['img']);
        //判断上传的返回结果
        if($info){
          //设置表中filepath字段
          $post['url'] =  $info['savepath'] . $info['savename'];
          //设置表中hasfile字段
          $post['ctime'] = time();
          $rst = M('news')->add($post);
          if($rst){
            $this -> success('添加成功',U('newsList'));
          }else{
            $this -> error('添加失败，请重新添加！！');
          }
        }else{
           $this -> error('添加失败，请重新添加！！');
        }
      
      }else{
         $this -> display();
      }
   }
  public function linianAdd(){
    if(IS_POST){
     $post = I('post.');
        $cfg = array(
          'rootPath' => WORKING_PATH . UPLOAD_ROOT_PATH
          );
        //
        $upload = new \Think\Upload($cfg);
        $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        //上传
        $info = $upload -> uploadOne($_FILES['img']);
        //判断上传的返回结果
        if($info){
          //设置表中filepath字段
          $post['url'] =  $info['savepath'] . $info['savename'];
          //设置表中hasfile字段
          $post['ctime'] = time();
          $rst = M('linian')->add($post);
          if($rst){
            $this -> success('添加成功',U('linianList'));
          }else{
            $this -> error('添加失败，请重新添加！！');
          }
        }else{
           $this -> error('添加失败，请重新添加！！');
        }
    }else{
        $this -> display();
    }
  }
  public function linianList(){
    $info = M('linian')->select();
    foreach ($info as $key => $value) {
       $info[$key]['time'] = date('Y-m-d H:i:s',$value['ctime']);
    }
    $this -> assign('info',$info);
    $this -> display();
  }
  public function linianDel(){
    $rst = M('linian')->where('id='.I('get.id'))->delete();
         if($rst){
            $this -> success('删除成功！',U('linianList'));
         }else{
               $this -> error('删除失败，请重新删除！！');
         }
  } 
 public function productAdd(){

    if(IS_POST){
     $post = I('post.');
        $cfg = array(
          'rootPath' => WORKING_PATH . UPLOAD_ROOT_PATH
          );
        //
        $upload = new \Think\Upload($cfg);
        $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        //上传
        $info = $upload -> uploadOne($_FILES['img']);
        //判断上传的返回结果
        if($info){
          //设置表中filepath字段
          $post['url'] =  $info['savepath'] . $info['savename'];
          //设置表中hasfile字段
          $post['ctime'] = time();
          $rst = M('product')->add($post);
          if($rst){
            $this -> success('添加成功',U('productList'));
          }else{
            $this -> error('添加失败，请重新添加！！');
          }
        }else{

           $this -> error('添加失败，请重新添加！！');
        }
    }else{
        $info = M('fenlei')->select();
        $this -> assign('info',$info);
        $this -> display();
    }
  }
  public function productList(){
    $info = M('product')->select();
    foreach ($info as $key => $value) {
       $info[$key]['time'] = date('Y-m-d H:i:s',$value['ctime']);
    }
    $this -> assign('info',$info);
    $this -> display();
  }
  public function productDel(){
    $rst = M('product')->where('id='.I('get.id'))->delete();
         if($rst){
            $this -> success('删除成功！',U('productList'));
         }else{
               $this -> error('删除失败，请重新删除！！');
         }
  }

 public function fenleiAdd(){

    if(IS_POST){
     $post = I('post.');
        $cfg = array(
          'rootPath' => WORKING_PATH . UPLOAD_ROOT_PATH
          );
        //
        $upload = new \Think\Upload($cfg);
        $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        //上传
        $info = $upload -> uploadOne($_FILES['img']);
        //判断上传的返回结果
        if($info){
          //设置表中filepath字段
          $post['url'] =  $info['savepath'] . $info['savename'];
          //设置表中hasfile字段
          $post['ctime'] = time();
          $rst = M('fenlei')->add($post);
          if($rst){
            $this -> success('添加成功',U('fenleiList'));
          }else{
            $this -> error('添加失败，请重新添加！！');
          }
        }else{

           $this -> error('添加失败，请重新添加！！');
        }
    }else{
        $info = M('fenlei')->select();
        $this -> assign('info',$info);
        $this -> display();
    }
  }
  public function fenleiList(){
    $info = M('fenlei')->select();
    foreach ($info as $key => $value) {
       $info[$key]['time'] = date('Y-m-d H:i:s',$value['ctime']);
    }
    $this -> assign('info',$info);
    $this -> display();
  }
  public function fenleiDel(){
    $rst = M('fenlei')->where('id='.I('get.id'))->delete();
         if($rst){
            $this -> success('删除成功！',U('fenleiList'));
         }else{
               $this -> error('删除失败，请重新删除！！');
         }
  }
  public function questionList(){
    $info =M('question')->select();
    $i=1;
    foreach($info as $k => $v){
      $info[$k]['sorce'] = $i;
      $i++;
    }
    $this ->assign('info',$info);
    $this ->display();
  }
  public function questionDel(){
    $info = M('question')->where(array('id'=>I('get.id')))->delete();
    if($info){
      $this -> success('删除成功',U('questionList'));
    }else{
      $this -> error('删除失败');
    }
  }
  public function questionAdd(){
    if(IS_POST){
    $data = I('post.');
    $info = M('question')->add($data);
    if($info){
      $this -> success('添加成功',U('questionList'));
    }else{
      $this -> error('添加失败');
    }
    }else{
      $this -> display();
    }
   
  }
  public function  fenleiEdite(){
    if(IS_POST){
      $data = I('post.');
    $info = M('fenlei')->save($data);
      if($info !== false){
          $this -> success('修改成功',U('fenleiList'));
      }else{
          $this -> error('修改失败');
      }
    }else{  
      $id = I('get.id');
      $fenlei = M('fenlei')->where(array('id'=>$id))->find();
       $this -> assign('id',$id);
       $this -> assign('img',$fenlei);
       $this -> display();
    }
  }
 public function  newsEdite(){
    if(IS_POST){
      $data = I('post.');
        $info = M('news')->save($data);
      if($info !== false){
          $this -> success('修改成功',U('newsList'));
      }else{
          $this -> error('修改失败');
      }
    }else{
        $id = I('get.id'); 
      $news = M('news')->where(array('id'=>$id))->find();
       $this -> assign('id',$id);
       $this -> assign('img',$news);
       $this -> display();
    }
  }
  public function  linianEdite(){
    if(IS_POST){
      $data = I('post.');
    $info = M('linian')->save($data);
      if($info !== false){
          $this -> success('修改成功',U('linianList'));
      }else{
          $this -> error('修改失败');
      }
    }else{
       $id = I('get.id');
       $linian = M('linian')->where(array('id'=>$id))->find();
       $this -> assign('id',$id);
       $this -> assign('img',$linian);
       $this -> display();
    }
  }
  public function  productEdite(){
    if(IS_POST){
      $data = I('post.');
    $info = M('product')->save($data);
      if($info !== false){
          $this -> success('修改成功',U('productList'));
      }else{
          $this -> error('修改失败');
      }
    }else{
      $info = M('fenlei')->select();
        $this -> assign('info',$info);
       $id = I('get.id');
       $product = M('product')->where(array('id'=>$id))->find();
       $this -> assign('id',$id);
       $this -> assign('img',$product);
       $this -> display();
    }
  }
  public function  questionEdite(){
    if(IS_POST){
      $data = I('post.');
    $info = M('question')->save($data);
      if($info !== false){
          $this -> success('修改成功',U('questionList'));
      }else{
          $this -> error('修改失败');
      }
    }else{
      $info = M('question')->select();
        $this -> assign('info',$info);
       $id = I('get.id');
       $question = M('question')->where(array('id'=>$id))->find();
       $this -> assign('id',$id);
       $this -> assign('img',$question);
       $this -> display();
    }
  }
} 